/*
 *  Class for java like code editor panel. */
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.filechooser.FileFilter;

import jsyntaxpane.DefaultSyntaxKit;

public class CodeEditor extends JPanel {

	JPanel editorPanel;
	private JScrollPane editorScrollPane;
	private JScrollPane buttonScrollPane;
	private JEditorPane editorPane;
	private JPanel buttonPanel;
	private JButton saveButton;
	private JButton loadButton;
	private JSplitPane splitpane;

	public CodeEditor()

	{
		editorPanel = new JPanel();
		editorPanel.setLayout(new BorderLayout());

		editorPane = new JEditorPane();
		editorPane.setVisible(true);
		DefaultSyntaxKit.initKit();

		// editorPane.setEditorKit(new SyntaxKit("java"));
		// editorPane.setSize(400, 400);

		editorScrollPane = new JScrollPane(editorPane);
		editorScrollPane.setPreferredSize(new Dimension(200, 200));

		editorPane.setContentType("text/java");
		saveButton = new JButton();
		saveButton.setText("Save File");

		final JFileChooser saveLocationChooser = new JFileChooser();
		saveButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				FileFilter type1 = new ExtensionFilter("java source", ".java");
				saveLocationChooser.addChoosableFileFilter(type1);
				int option = saveLocationChooser
						.showSaveDialog(CodeEditor.this);
				if (option == JFileChooser.APPROVE_OPTION) {
					if (saveLocationChooser.getSelectedFile() != null) {
						File theFileToSave = saveLocationChooser
								.getSelectedFile();
						try {
							saveContent(theFileToSave);
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}

					}
				}

			}

			private void saveContent(File theFileToSave) throws IOException {
				// TODO Auto-generated method stub
				Writer out = new BufferedWriter(new OutputStreamWriter(
						new FileOutputStream(theFileToSave)));
				// FileOutputStream fop=new FileOutputStream(f);

				out.write(editorPane.getText());
				out.flush();
				out.close();
			}

		});
		loadButton = new JButton();
		loadButton.setText("Load File");
		final JFileChooser loadLocationChooser = new JFileChooser();
		loadButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				FileFilter type1 = new ExtensionFilter("Java Source", ".java");
				loadLocationChooser.addChoosableFileFilter(type1);
				int option = loadLocationChooser
						.showOpenDialog(CodeEditor.this);
				if (option == JFileChooser.APPROVE_OPTION) {
					if (loadLocationChooser.getSelectedFile() != null) {
						File theFileToSave = loadLocationChooser
								.getSelectedFile();

						loadFile(theFileToSave);
						;

					}
				}

				
			}

			private void loadFile(File theFileToSave) {
				
				StringBuilder contents = new StringBuilder();

				try {
					BufferedReader in = new BufferedReader(new FileReader(
							theFileToSave));
					String str = null;
					while ((str = in.readLine()) != null) {
						contents.append(str);
						contents.append(System.getProperty("line.separator"));
					}
					// System.out.println(contents.toString());
					editorPane.setContentType("text/java");
					editorPane.setText(contents.toString());

					in.close();
				} catch (IOException e) {
				}

			}
		});

		buttonPanel = new JPanel();
		buttonPanel.add(loadButton);
		buttonPanel.add(saveButton);

		buttonScrollPane = new JScrollPane(buttonPanel);

		splitpane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, editorScrollPane,
				buttonScrollPane);
		splitpane.setOneTouchExpandable(true);
		splitpane.setDividerLocation(500);

		// editorScrollPane.add(editorPane);
		editorPanel.add(splitpane);
	}

}
